<?php

if (isset($_GET['session']) && isset($_GET['time']))
{
	$time = $_GET['time'];
	$session = $_GET['session'];
}
if ( isset($session) && isset($time))
{
	if (time() - $time > 10*60)
	{
		$url = 'http://ade52-savoie.grenet.fr/ade/webapi?function=connect&login=ETUDIANT&password=etsa';
		$agent = '';
		$idCookies = '';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 2);   
		curl_setopt($ch, CURLOPT_COOKIE, $idCookies);
		$ret = curl_exec($ch);
		$erreurRecup = curl_errno($ch);
		if (!$erreurRecup &&(strpos($ret, '<session') === false))
			$erreurRecup = true;
		
		if (!$erreurRecup)
		{
			curl_close($ch);
			$page = new DOMDocument();
			$page->loadXML($ret);
			
			$session = $page->getElementsByTagName("session")->item(0)->attributes->item(0)->value;
			$time = time();
			
			$url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=setProject&projectId=4";
		
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_USERAGENT, $agent);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 4);   
			curl_setopt($ch, CURLOPT_COOKIE, $idCookies);
			$ret = curl_exec($ch);
			if (curl_errno($ch))
			{
				$session = -1;
				$time = -1;
			}
			else
				curl_close($ch);
		}
		else
		{
			$session = -1;
			$time = -1;
		}
	}
	
	echo "<input type='hidden' id='session' value='$session' />";
	echo "<input type='hidden' id='time' value='$time' />";
}
?>








